package com.taptech.doufu.ui.view.topicview;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.DFHomeNovelBeans;
import com.taptech.doufu.bean.DFHomeNovelContentBeans;
import com.taptech.doufu.bean.EventBean;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.ui.adapter.DfhomeNovelAdapter3;
import com.taptech.doufu.ui.view.topicview.adapter.NovelTopicRankAdapter;

import java.util.List;

/**
 * Created by Administrator on 2017/4/13/013.
 */
public class NovelTopicRankView extends LinearLayout{
    private TextView tvTitle;
    private View llMore;
    private String moreUrl;
    private String title;
    private String type;

    private List<DFHomeNovelBeans> list;
    public NovelTopicRankView(Context context) {
        super(context);
    }

    public NovelTopicRankView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NovelTopicRankView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    private View root_layout;
    private View linearLayout0;
    private TextView tv_more;
    private Context context;
    private EventBean eventBean;
    private RecyclerView mRecyclerView;

    public NovelTopicRankView(final Context context, DFHomeNovelContentBeans model, final String type, ContentType contentType,String pageType){
        this(context);
        View view = LayoutInflater.from(context).inflate(R.layout.topic_novel_rank_grid_view, null);
        this.context = context;
        this.type = type;
        linearLayout0 = view.findViewById(R.id.home_dfnovel_layout0);
        mRecyclerView = view.findViewById(R.id.mRecyclerView);
        tv_more = (TextView) view.findViewById(R.id.tv_more);
        root_layout = view.findViewById(R.id.root_layout);
        tvTitle = (TextView) view.findViewById(R.id.tvTitle);
        llMore = view.findViewById(R.id.llMore);
        tvTitle.setText(model.getTitle());
        moreUrl = model.getMore_url();
        title = model.getTitle();
        eventBean = model.getEventData();
        changeDayModelBg();
        list = model.getList();
        llMore.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                UmengEventUtil.addRightTopBtnEvent(context, eventBean, type,pageType);
                if(moreUrl!=null && moreUrl.contains("/weex/")){
                    SimpleWeexActivity.startActivity(context,moreUrl);
                }else{
                    DfhomeNovelAdapter3.goToNovelMoreActivity(context,moreUrl,title,type);
                }
            }
        });
        if(!TextUtils.isEmpty(model.getMore_url())){
            llMore.setVisibility(VISIBLE);
        }else{
            llMore.setVisibility(GONE);
        }
        setData(list);
        addView(view);
    }


    private void setData(List<DFHomeNovelBeans> list){
        if (list != null) {
            NovelTopicRankAdapter adapter = new NovelTopicRankAdapter(context,list,eventBean);
            MeasuredGridLayoutManager gridLayoutManager = new MeasuredGridLayoutManager(getContext(),2);
            gridLayoutManager.setScrollEnabled(false);
            mRecyclerView.setLayoutManager(gridLayoutManager);
            mRecyclerView.setAdapter(adapter);
        }
    }



    public void changeDayModelBg(){
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            darkBg();
        }else {
            dayBg();
        }
    }


    private void dayBg() {
        root_layout.setBackgroundColor(context.getResources().getColor(R.color.home_list_item_color));
        linearLayout0.setBackgroundColor(context.getResources().getColor(R.color.white));
        tvTitle.setTextColor(context.getResources().getColor(R.color.text_color_1));
        tv_more.setTextColor(context.getResources().getColor(R.color.text_color_6C788A));
    }


    private void darkBg() {
        root_layout.setBackgroundColor(context.getResources().getColor(R.color.bg_dark));
        linearLayout0.setBackgroundColor(context.getResources().getColor(R.color.fg_dark));
        tvTitle.setTextColor(context.getResources().getColor(R.color.title_text_dark));
        tv_more.setTextColor(context.getResources().getColor(R.color.text_dark));
        tv_more.setTextColor(context.getResources().getColor(R.color.text_dark));
    }

}
